package com.jt.service;

import com.jt.mapper.MovieDescMapper;
import com.jt.mapper.MovieMapper;
import com.jt.pojo.Movie;
import com.jt.pojo.MovieDesc;
import com.jt.vo.MovieVo;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MovieServiceImpl implements MovieService{

    @Autowired
    private MovieMapper movieMapper;

    @Autowired
    private MovieDescMapper movieDescMapper;



    @Override
    public PageResult getMovieList(PageResult pageResult) {
        // 查询电影总条数
        long total = movieMapper.getTotal();
        // 每页的限制条件  当页开始位置和当前页显示多少条
        int size = pageResult.getPageSize();
        int start = (pageResult.getPageNum()-1)*size;
        // 查询条件
        String query = pageResult.getQuery();
        // 根据分页条件 查询movie库
        List<Movie> movieList = movieMapper.findMovieByPage(size,start,query);
        // 查询movie_desc库的所有数据
        List<MovieDesc> movieDescList = movieDescMapper.findAllMovieDesc();
        // 创建两个库的整合对象集合
        List<MovieVo> movieVoList = new ArrayList<>();
        // 遍历
        for(Movie movie : movieList){
            for(MovieDesc movieDesc : movieDescList){
                MovieVo movieVo = new MovieVo();
                if (movie.getId() == movieDesc.getId()){
                    movieVo.setMovie(movie).setMovieDesc(movieDesc);
                    movieVoList.add(movieVo);
                    break;
                }
            }
        }
        return pageResult.setTotal(total).setRows(movieVoList);
    }
}
